﻿using System.Configuration;

namespace com.uplus.common
{
    /// <summary>
    /// 提供配置更新服务
    /// </summary>
    public static class CfgMgr
    {
        public static void AppSettingsSet(string key, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] != null)
            {
                config.AppSettings.Settings[key].Value = value;
            }

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        public static T AppSettionsGet<T>(string key, T defaultValue)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] != null)
            {
                return Converter.To<T>(config.AppSettings.Settings[key].Value, defaultValue);
            }
            return defaultValue;
        }
    }
}
